﻿<?php
	if(Online)
	{
		if(isset($_GET['id']))
		{
			$id = $_GET['id'];
		}
		if(isset($_GET['action']))
		{
			$action = $_GET['action'];
		}
		
		if((isset($action)) AND ($action == 'ajout'))
		{
			if((isset($_POST['redList']) AND ($_POST['redList'] == 'on'))){$redList = 1;}else{$redList = 0;}
			if((isset($_POST['diffus']) AND ($_POST['diffus'] == 'on'))){$diffus = 1;}else{$diffus = 0;}
			if((isset($_POST['benevole']) AND ($_POST['benevole'] == 'on'))){$benevole = 1;}else{$benevole = 0;}
			if((isset($_POST['travail']) AND ($_POST['travail'] == 'on'))){$travail = 1;}else{$travail = 0;}
			if((isset($_POST['adherent']) AND ($_POST['adherent'] == 'on'))){$adherent = 1;}else{$adherent = 0;}
			if((isset($_POST['checkboxD']) AND ($_POST['checkboxD'] == 'on'))){$dirigeant = 1;}else{$dirigeant = 0;}
			if((isset($_POST['checkboxE']) AND ($_POST['checkboxE'] == 'on'))){$encadrant = 1;}else{$encadrant = 0;}
			if((isset($_POST['arbitre']) AND ($_POST['arbitre'] == 'on'))){$arbitre = 1;}else{$arbitre = 0;}
			if((isset($_POST['marqueur']) AND ($_POST['marqueur'] == 'on'))){$marqueur = 1;}else{$marqueur = 0;}
			if((isset($_POST['entraineur']) AND ($_POST['entraineur'] == 'on'))){$entraineur = 1;}else{$entraineur = 0;}
			if((isset($_POST['coach']) AND ($_POST['coach'] == 'on'))){$coach = 1;}else{$coach = 0;}
			if((isset($_POST['bureau']) AND ($_POST['bureau'] == 'on'))){$bureau = 1;}else{$bureau = 0;}
			if((isset($_POST['cotisation']) AND ($_POST['cotisation'] == 'on'))){$cotisation = 1;}else{$cotisation = 0;}
			if((isset($_POST['licence']))){$licence = $_POST['licence'];}else{$licence = "000000";}
			if((isset($_POST['idsaison']) AND ($_POST['idsaison'] != ''))){$idsaison = $_POST['idsaison'];}else{$idsaison = '1';}
			
			if((isset($_POST['nom']))){$nom = $_POST['nom'];}else{$nom = "";}
			if((isset($_POST['dateNaiss'])) && ($_POST['dateNaiss'] != '')){$dateNaiss = $_POST['dateNaiss'];}else{$dateNaiss = "0000-00-00";}
			if((isset($_POST['prenom']))){$prenom = $_POST['prenom'];}else{$prenom = "";}
			if((isset($_POST['civilite'])))
			{
				$civilite = $_POST['civilite'];
				if(($civilite == 'Mme') || ($civilite == 'Mlle')){$sexe = 'F';}else{$sexe = 'M';}
			}
			else
			{
				$civilite = "";
				$sexe = "";
			}
			if((isset($_POST['mail']))){$mail = $_POST['mail'];}else{$mail = "";}
			if((isset($_POST['orga']))){$orga = $_POST['orga'];}else{$orga = "";}
			if((isset($_POST['adr']))){$adr = $_POST['adr'];}else{$adr = "";}
			if((isset($_POST['compAdre']))){$compAdre = $_POST['compAdre'];}else{$compAdre = "";}
			if((isset($_POST['cp']))){$cp = $_POST['cp'];}else{$cp = "";}
			if((isset($_POST['telF']))){$telF = $_POST['telF'];}else{$telF = "";}
			if((isset($_POST['ville']))){$ville = $_POST['ville'];}else{$ville = "";}
			if((isset($_POST['telP']))){$telP = $_POST['telP'];}else{$telP = "";}
			if((isset($_POST['responsable'])) && ($_POST['responsable'] != '')){$responsable = $_POST['responsable'];}else{$responsable = NULL;}
			
			// PARTIE REQUÊTE ===========================================================================================================================================
			
			// Requête de MàJ sur la table Contact:
			$AddC = $bdd->prepare("INSERT IGNORE INTO contact (nomContact, prenomContact, datedenaissanceContact, sexeContact, civiliteContact, adresseContact, complementAdresse, cpContact, villeContact ,telfixeContact, telportableContact, mailContact, listerouge, pasdediffusion, estBenevole)
								   VALUES ('$nom', '$prenom', '$dateNaiss', '$sexe', '$civilite', '$adr', '$compAdre', $cp, '$ville', $telF, $telP, '$mail', $redList, $diffus, $benevole)");
			$AddC->execute();
			
			// requête de MàJ sur la table Adherent:
			//$AddA = $bdd->prepare("INSERT IGNORE INTO adherent (numLicenceAdherent, peutArbitrer, peutMarquer, peutEntrainer, peutCoacher, estdirigeant, estEncadrant, estMembreDuBureau)
			//						VALUES ('$licence', $arbitre, $marqueur, $entraineur, $coach, $dirigeant, $encadrant, $bureau)");
			
			$reqContact = $bdd->prepare("SELECT * FROM contact C WHERE nomContact = '$nom' AND prenomContact = '$prenom'");
			$reqContact ->execute();
			
			if($data = $reqContact->fetch())
			{
				$idC = $data['idContact'];
				$test = "INSERT IGNORE INTO adherent (numLicenceAdherent, peutArbitrer, peutMarquer, peutEntrainer, peutCoacher, estdirigeant, estEncadrant, estMembreDuBureau, idContact)
						 VALUES ('$licence', $arbitre, $marqueur, $entraineur, $coach, $dirigeant, $encadrant, $bureau, $idC)";
			}
			
			echo "Les informations ont été correctement insérées.";
		}
		// PARTIE MODIFICATION ===========================================================================================
		elseif((isset($action)) AND ($action == 'modifUser'))
		{
			
			if((isset($_POST['redList']) AND ($_POST['redList'] == 'on'))){$redList = 1;}else{$redList = 0;}
			if((isset($_POST['diffus']) AND ($_POST['diffus'] == 'on'))){$diffus = 1;}else{$diffus = 0;}
			if((isset($_POST['benevole']) AND ($_POST['benevole'] == 'on'))){$benevole = 1;}else{$benevole = 0;}
			if((isset($_POST['travail']) AND ($_POST['travail'] == 'on'))){$travail = 1;}else{$travail = 0;}
			if((isset($_POST['adherent']) AND ($_POST['adherent'] == 'on'))){$adherent = 1;}else{$adherent = 0;}
			if((isset($_POST['checkboxD']) AND ($_POST['checkboxD'] == 'on'))){$dirigeant = 1;}else{$dirigeant = 0;}
			if((isset($_POST['checkboxE']) AND ($_POST['checkboxE'] == 'on'))){$encadrant = 1;}else{$encadrant = 0;}
			if((isset($_POST['arbitre']) AND ($_POST['arbitre'] == 'on'))){$arbitre = 1;}else{$arbitre = 0;}
			if((isset($_POST['marqueur']) AND ($_POST['marqueur'] == 'on'))){$marqueur = 1;}else{$marqueur = 0;}
			if((isset($_POST['entraineur']) AND ($_POST['entraineur'] == 'on'))){$entraineur = 1;}else{$entraineur = 0;}
			if((isset($_POST['coach']) AND ($_POST['coach'] == 'on'))){$coach = 1;}else{$coach = 0;}
			if((isset($_POST['bureau']) AND ($_POST['bureau'] == 'on'))){$bureau = 1;}else{$bureau = 0;}
			if((isset($_POST['cotisation']) AND ($_POST['cotisation'] == 'on'))){$cotisation = 1;}else{$cotisation = 0;}
			if((isset($_POST['licence']))){$licence = $_POST['licence'];}else{$licence = "000000";}
			if((isset($_POST['idsaison']) AND ($_POST['idsaison'] != ''))){$idsaison = $_POST['idsaison'];}else{$idsaison = '1';}
			
			if((isset($_POST['nom']))){$nom = $_POST['nom'];}else{$nom = "";}
			if((isset($_POST['dateNaiss'])) && ($_POST['dateNaiss'] != '')){$dateNaiss = $_POST['dateNaiss'];}else{$dateNaiss = "0000-00-00";}
			if((isset($_POST['prenom']))){$prenom = $_POST['prenom'];}else{$prenom = "";}
			
			if((isset($_POST['civilite'])))
			{
				$civilite = $_POST['civilite'];
				if(($civilite == 'Mme') || ($civilite == 'Mlle')){$sexe = 'F';}else{$sexe = 'M';}
			}
			else
			{
				$civilite = "";
				$sexe = "";
			}
			
			if((isset($_POST['mail']))){$mail = $_POST['mail'];}else{$mail = "";}
			if((isset($_POST['orga']))){$orga = $_POST['orga'];}else{$orga = "";}
			if((isset($_POST['adr']))){$adr = $_POST['adr'];}else{$adr = "";}
			if((isset($_POST['compAdre']))){$compAdre = $_POST['compAdre'];}else{$compAdre = "";}
			if((isset($_POST['cp']))){$cp = $_POST['cp'];}else{$cp = "";}
			if((isset($_POST['telF'])) && ($_POST['telF'] != '')){$telF = $_POST['telF'];}else{$telF = '0000000000';}
			if((isset($_POST['ville']))){$ville = $_POST['ville'];}else{$ville = "";}
			if((isset($_POST['telP'])) && ($_POST['telP'] != '')){$telP = $_POST['telP'];}else{$telP = '0000000000';}
			
			
			
			// PARTIE REQUÊTE ===========================================================================================================================================
			
			// Requête de MàJ sur la table Contact:
			$UpdateC = $bdd->prepare("UPDATE contact C, organisation O 
									  SET nomContact = '$nom', prenomContact = '$prenom', datedenaissanceContact = '$dateNaiss', sexeContact = '$sexe', civiliteContact = '$civilite', adresseContact = '$adr', complementAdresse = '$compAdre', cpContact = $cp, villeContact = '$ville', telfixeContact = $telF, telportableContact = $telP, mailContact = '$mail', listerouge = $redList, pasdediffusion = $diffus, estBenevole = $benevole, C.idOrganisation  = '$orga' 
									  WHERE C.idContact = $id 
									  AND C.idOrganisation = O.idOrganisation");
			
			// requête de MàJ sur la table Adherent:
			$UpdateA = $bdd->prepare("UPDATE adherent A, contact C 
									  SET numLicenceAdherent = '$licence', peutArbitrer = $arbitre, peutMarquer = $marqueur, peutEntrainer = $entraineur, peutCoacher = $coach, estdirigeant = $dirigeant, estEncadrant = $encadrant, estMembreDuBureau = $bureau 
									  WHERE A.idContact = C.idContact 
									  AND C.idContact = $id");
			
			// requête de MàJ sur la table Reglement:
			$UpdateR = $bdd->prepare("UPDATE reglement R, saison S, adherent A 
									  SET R.idAdherent = '$licence', R.idSaison = $idsaison, R.payeCotisation = $cotisation 
									  WHERE R.idAdherent = A.numLicenceAdherent 
									  AND R.idSaison = S.idSaison");
			
			
			$UpdateC->execute();
			$UpdateA->execute();
			//$UpdateR->execute();
			echo"La modification a bien été effectuée.";

		}
		// PARTIE SUPPRESSION =======================================================================================================
		elseif((isset($action)) AND ($action == 'suppr'))
		{
			if(isset($id))
			{
				$reqNumLicence = $bdd->prepare("SELECT * FROM adherent A, contact C WHERE A.idContact = C.idContact AND A.idContact = $id");
				$reqNumLicence ->execute();
				
				if($data = $reqNumLicence->fetch())
				{
					$licence = $data['numLicenceAdherent'];
					$deleteA = $bdd -> prepare("DELETE FROM adherent WHERE numLicenceAdherent = $licence");
					$deleteA ->execute();
				}
				
				$deleteC = $bdd -> prepare("DELETE FROM contact WHERE idContact = $id");
				
				$deleteC -> execute();
				
				echo"Les informations ont bien été supprimées.";
			}
			
		}
		//var_dump($_POST);
	}
	else
	{
		echo noAccess;
	}
?>